Skip to content

chore(spanner): restore active transaction state before retrying aborted commit - #15075

Open
sakthivelmanii wants to merge 2 commits into
mainfrom
chore-spanner-transaction-runner-commit-abort-retry
Open

sakthivelmanii wants to merge 2 commits into
mainfrom
chore-spanner-transaction-runner-commit-abort-retry

Conversation

@sakthivelmanii

Copy link
Copy Markdown
Contributor

Summary

Restores active read/write transaction state when Connection.runTransaction(...) retries after commit() encounters an AbortedException, ensuring retry attempts execute atomically within a single read/write transaction rather than falling back to autocommit mode.

What Changed & Why

  • Preserve transactional atomicity across commit-abort retries: Previously, an AbortedException during commit() cleared the connection's active transaction markers before entering the retry loop. When autocommit was enabled, statements in the next retry attempt executed as individual autocommit operations instead of a single atomic read/write transaction, and the retry commit failed with FAILED_PRECONDITION. Retrying now restores active transaction state, rolls back uncommitted transaction-local connection properties, clears buffered mutations and savepoints from the aborted attempt, and reuses the underlying transaction session and tracing span across retries.
  • Clean failure and cancellation teardown: Non-abort commit errors, swallowed statement aborts, and thread interruptions during commit or retry backoff now clean up connection state, active batches, session resources, and OpenTelemetry spans without masking the original error with a secondary FAILED_PRECONDITION.
  • Behavior change for aborts without server retry metadata: If a transaction callable throws a synthetic AbortedException (or an abort arrives without RetryInfo metadata), Connection.runTransaction(...) now retries using jittered exponential backoff (capped at 32 seconds) instead of failing with an invalid sleep delay or null transaction state.

…ted commit

### Summary
Restores active read/write transaction state when `Connection.runTransaction(...)` retries after `commit()` encounters an `AbortedException`, ensuring retry attempts execute atomically within a single read/write transaction rather than falling back to autocommit mode.

### What Changed & Why
- **Preserve transactional atomicity across commit-abort retries**: Previously, an `AbortedException` during `commit()` cleared the connection's active transaction markers before entering the retry loop. When `autocommit` was enabled, statements in the next retry attempt executed as individual autocommit operations instead of a single atomic read/write transaction, and the retry commit failed with `FAILED_PRECONDITION`. Retrying now restores active transaction state, rolls back uncommitted transaction-local connection properties, clears buffered mutations and savepoints from the aborted attempt, and reuses the underlying transaction session and tracing span across retries.
- **Clean failure and cancellation teardown**: Non-abort commit errors, swallowed statement aborts, and thread interruptions during commit or retry backoff now clean up connection state, active batches, session resources, and OpenTelemetry spans without masking the original error with a secondary `FAILED_PRECONDITION`.
- **Behavior change for aborts without server retry metadata**: If a transaction callable throws a synthetic `AbortedException` (or an abort arrives without `RetryInfo` metadata), `Connection.runTransaction(...)` now retries using jittered exponential backoff (capped at 32 seconds) instead of failing with an invalid sleep delay or null transaction state.
@sakthivelmanii
sakthivelmanii requested review from a team as code owners November 18, 2026 22:37
@sakthivelmanii sakthivelmanii self-assigned this Nov 18, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request enhances the transaction retry and abort handling mechanism in the Spanner connection API. It introduces a configuration to keep aborted transactions open so that they can be cleanly reset or rolled back by the transaction runner, and implements a jittered exponential backoff for synthetic aborts that lack a retry delay. The review feedback highlights two important improvements: first, a potential IllegalStateException when resetting the transaction manager during synthetic aborts because resetForRetry() requires the manager to be in the ABORTED state; second, the need to populate this.abortedException in handleAborted when keeping aborted transactions open to prevent null pointer or state issues downstream.

Comment on lines 1341 to 1350
if (!keepAbortedTransactionOpen && txManager != null) {
try {
txManager.close();
} catch (Throwable t) {
// ignore
}
txManager = null;
}
// Internal retry is not enabled.
this.state = UnitOfWorkState.ABORTED;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

In handleAborted(AbortedException aborted), when keepAbortedTransactionOpen is true, the transaction state transitions to ABORTED but this.abortedException is not populated. This could lead to abortedException being null when TransactionRunnerImpl or other components expect it to be populated. We should set this.abortedException = aborted here as well for robustness.

      if (!keepAbortedTransactionOpen && txManager != null) {
        try {
          txManager.close();
        } catch (Throwable t) {
          // ignore
        }
        txManager = null;
      }
      // Internal retry is not enabled.
      this.state = UnitOfWorkState.ABORTED;
      if (keepAbortedTransactionOpen) {
        this.abortedException = aborted;
      }

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant